package com.bocaizp.operation.controller.admin;

import com.bocaizp.common.constant.JwtClaimsConstant;
import com.bocaizp.common.properties.JwtProperties;
import com.bocaizp.common.result.Result;
import com.bocaizp.common.utils.JwtUtil;
import com.bocaizp.operation.mapper.LoginMapper;
import com.bocaizp.operation.service.LoginService;
import com.bocaizp.pojo.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Author : huang jie
 * Date : 2024/5/13 18:43
 **/
@RestController
@RequestMapping("/admin")
@Slf4j
@CrossOrigin
public class AdminLoginController {

	@Autowired
	private LoginService loginService;
	@Autowired
	private LoginMapper loginMapper;
	@Autowired
	private JwtProperties jwtProperties;


	/**
	 * 员工登录
	 * @param username
	 * @param password
	 * @return
	 */
	@GetMapping("/login")
	public Result<String> loginEmployee(String empID,String password){
		log.info("管理端登录请求...");
		System.out.println(empID+":" +password);
		Employee employee = loginService.loginEmployee(empID,password);

		Map<String, Object> claim = new HashMap<>();
		claim.put(JwtClaimsConstant.EMP_ID, employee.getEmpId());
		claim.put(JwtClaimsConstant.POWER, employee.getEmpPower());
		//生成jwt令牌
		String token = JwtUtil.createJWT(
				jwtProperties.getAdminSecretKey(),
				jwtProperties.getAdminTtl(),
				claim);
		//封装对象
		System.out.println(token);
		return Result.success(token);
	}
}
